﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using HeThongThongTin.Library;
using Converter;
using System.Resources;
using System.Reflection;
using HeThongThongTin.UI.SL.HeThong.HT_DangKy;
using HeThongThongTin.UI.SL.HeThong.HT_QuanTriTaiKhoan;
using HeThongThongTin.UI.SL.HeThong.HT_VaiTro;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using Telerik.Windows.Controls;


namespace HeThongThongTin.UI.SL.HeThong.HT_QuanLyVaiTroQuyenHan
{
    public partial class HT_QuanLyVaiTroQuyenHan_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = 0;
            }
        }

        public string Title
        {
            get { return "QuanLyVaiTroQuyenHan"; }
        }

        #endregion
        private int Khoa = 0;
      
        List<int> ListQuyenHanNeedInsert = new List<int>();

        #region Load Methods
        private List<RadTreeViewItem> ListBangDuLieuItem = new List<RadTreeViewItem>();
        List<HT_QuyenHan_ListViewModel> listQuyenHanViewModel = new List<HT_QuyenHan_ListViewModel>();
        
        List<HT_VaiTro_QuyenHan_ViewModel> NewListViewModel = new List<HT_VaiTro_QuyenHan_ViewModel>();
        
        public HT_QuanLyVaiTroQuyenHan_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("HeThongThongTin.UI.SL.HeThong.HT_QuanLyVaiTroQuyenHan.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.HT_QuanLyVaiTroQuyenHan_GeneralInformation_GroupBox, controls);
            this.HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_RadTreeView.ItemPrepared += new EventHandler<Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs>(HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_ItemPrepared);
            
        }

        public HT_QuanLyVaiTroQuyenHan_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource ht_VaiTro_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_ViewModelSource"];
                HT_VaiTro_ViewModel ht_VaiTro_ViewModel = new HT_VaiTro_ViewModel(Khoa);
                ht_VaiTro_ViewModel.LoadCompleted += new EventHandler(ht_VaiTro_ViewModel_LoadCompleted);
                ht_VaiTro_ViewSource.Source = new List<object> { ht_VaiTro_ViewModel };
               
                System.Windows.Data.CollectionViewSource ht_BangDuLieu_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_BangDuLieu_ListViewModelSource"];
                HT_BangDuLieu_ListViewModel ht_BangDuLieu_ListViewModel = new HT_BangDuLieu_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                ht_BangDuLieu_ListViewSource.Source = new List<object> { ht_BangDuLieu_ListViewModel };
                
              
            }
        }       
        #endregion

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        #region Save Methods

        private void SaveItemButton_Click(object sender, RoutedEventArgs e)
        {
            
            IList<object> listcheck = this.HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_RadTreeView.CheckedItems.ToList<object>();
                
            System.Windows.Data.CollectionViewSource ht_VaiTro_QuyenHan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_QuyenHan_ListViewModelSource"];
            HT_VaiTro_QuyenHan_ListViewModel ht_VaiTro_QuyenHan_ListViewModel = ((List<object>)ht_VaiTro_QuyenHan_ListViewSource.Source)[0] as HT_VaiTro_QuyenHan_ListViewModel;
            IList<HT_VaiTro_QuyenHanInfo> listVaiTroQuyenHan = ht_VaiTro_QuyenHan_ListViewModel.Model.getListOfInfo();


            List<int> ListQuyenHanCheck = new List<int>();
               
            foreach (object item in listcheck)
            {
                if (item is HT_QuyenHanInfo)
                {
                    ListQuyenHanCheck.Add(((HT_QuyenHanInfo)item).Khoa);

                    bool needInsert = true;
                    foreach (HT_VaiTro_QuyenHanInfo VaiTroQuyenHanItem in listVaiTroQuyenHan)
                    {
                        if(VaiTroQuyenHanItem.QuyenHanId == ((HT_QuyenHanInfo)item).Khoa)
                        {
                                
                            needInsert = false;
                            break;
                        }
                    }
                    if(needInsert)
                    {
                        ListQuyenHanNeedInsert.Add(((HT_QuyenHanInfo)item).Khoa);
                    }
                }
            }

                
            foreach (HT_VaiTro_QuyenHanInfo VaiTroQuyenHanItem in listVaiTroQuyenHan)
            {
                bool needdelete = true;
                foreach(int item in ListQuyenHanCheck)
                {
                        
                    if(item == VaiTroQuyenHanItem.QuyenHanId)
                    {
                        needdelete = false;
                        break;
                    }
                }
                if(needdelete)
                {
                    HeThongThongTin.Library.HT_VaiTro_QuyenHan.DeleteHT_VaiTro_QuyenHan(VaiTroQuyenHanItem.Khoa, (o, ex) =>
                    {
                    });
                        
                }
            }

            foreach (int item in ListQuyenHanNeedInsert)
            {
                System.Windows.Data.CollectionViewSource ht_VaiTro_QuyenHan_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_QuyenHan_ViewModelSource"];
                HT_VaiTro_QuyenHan_ViewModel ht_VaiTro_QuyenHan_ViewModel = new HT_VaiTro_QuyenHan_ViewModel();
                ht_VaiTro_QuyenHan_ViewModel.LoadCompleted += new EventHandler(ht_VaiTro_QuyenHan_ViewModel_LoadCompleted);
                ht_VaiTro_QuyenHan_ViewSource.Source = new List<object> { ht_VaiTro_QuyenHan_ViewModel };
                NewListViewModel.Add(ht_VaiTro_QuyenHan_ViewModel);

            }
            
        }

        private void ht_VaiTro_QuyenHan_ViewModel_LoadCompleted(object sender, EventArgs e)
        {

            if (AllLoadCompleted())
            {
                int index = 0;
                System.Windows.Data.CollectionViewSource ht_VaiTro_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_ViewModelSource"];
                HT_VaiTro_ViewModel ht_VaiTro_ViewModel = ((List<object>)ht_VaiTro_ViewSource.Source)[0] as HT_VaiTro_ViewModel;

                foreach (HT_VaiTro_QuyenHan_ViewModel item in NewListViewModel)
                {
                    item.Model.QuyenHanId = ListQuyenHanNeedInsert[index];
                    item.Model.VaiTroId = (ht_VaiTro_ViewModel.Model.Khoa);
                    index++;
                    item.Save(this, new Csla.Xaml.ExecuteEventArgs());
                }
                while (NewListViewModel.Count != 0)
                {
                    NewListViewModel.RemoveAt(0);
                }
                while (ListQuyenHanNeedInsert.Count != 0)
                {
                    ListQuyenHanNeedInsert.RemoveAt(0);
                }
            }
        }

        private bool AllLoadCompleted()
        {
            bool result = true;
            foreach (HT_VaiTro_QuyenHan_ViewModel item in NewListViewModel)
            {
                if (item.Model == null)
                {
                    result = false;
                }
            }
            return result;
        }

        #endregion


        #region Load TreeView

        void HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
        {
            RadTreeViewItem item = e.PreparedItem;
            if (item.Item is HT_BangDuLieuInfo)
            {
                ListBangDuLieuItem.Add(item);
                System.Windows.Data.CollectionViewSource ht_QuyenHan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_QuyenHan_ListViewModelSource"];
                HT_QuyenHan_ListViewModel ht_QuyenHan_ListViewModel = new HT_QuyenHan_ListViewModel(((HT_BangDuLieuInfo)item.Item).Khoa);
                ht_QuyenHan_ListViewModel.LoadCompleted += new EventHandler(ht_QuyenHan_ListViewModel_LoadCompleted);
                ht_QuyenHan_ListViewSource.Source = new List<object> { ht_QuyenHan_ListViewModel };
                listQuyenHanViewModel.Add(ht_QuyenHan_ListViewModel);

            }
            else if (item.Item is HT_QuyenHanInfo)
            {
                item.IsLoadOnDemandEnabled = false;
            }

        }

        private bool AllLoadQuyenHanListViewModelCompleted()
        {
            bool result = true;
            foreach (HT_QuyenHan_ListViewModel item in listQuyenHanViewModel)
            {
                if (item.Model == null)
                {
                    result = false;
                }
            }
            return result;
        }


        void ht_QuyenHan_ListViewModel_LoadCompleted(object sender, EventArgs e)
        {
            if (AllLoadQuyenHanListViewModelCompleted())
            {
                int index = 0;
                foreach (RadTreeViewItem item in ListBangDuLieuItem)
                {
                    item.ItemsSource = listQuyenHanViewModel[index].Model;
                    index++;
                }

               

            }

        }


        //private void RadTreeViewItem_Loaded(object sender, RoutedEventArgs e)
        //{

        //    foreach (RadTreeViewItem item in ListBangDuLieuItem)
        //    {
        //        System.Windows.Data.CollectionViewSource ht_QuyenHan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_QuyenHan_ListViewModelSource"];
        //        HT_QuyenHan_ListViewModel ht_QuyenHan_ListViewModel = new HT_QuyenHan_ListViewModel(((HT_BangDuLieuInfo)item.Item).Khoa);
        //        ht_QuyenHan_ListViewModel.LoadCompleted += new EventHandler(ht_QuyenHan_ListViewModel_LoadCompleted);
        //        ht_QuyenHan_ListViewSource.Source = new List<object> { ht_QuyenHan_ListViewModel };
        //        listQuyenHanViewModel.Add(ht_QuyenHan_ListViewModel);

        //    }
        //}

        # endregion

        #region Load Status QuyenHan
        private void ht_VaiTro_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            RadTreeViewItem item = this.HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_RadTreeView.Items[0] as RadTreeViewItem;
            if (!item.IsExpanded)
            {
                item.ExpandAll();
                item.Expanded += new EventHandler<RadRoutedEventArgs>(child_Expanded);
            }
        }

        bool allExpanded = false;
        void child_Expanded(object sender, EventArgs e)
        {
            if (!allExpanded)
            {
                System.Windows.Data.CollectionViewSource ht_VaiTro_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_ViewModelSource"];
                HT_VaiTro_ViewModel ht_VaiTro_ViewModel = ((List<object>)ht_VaiTro_ViewSource.Source)[0] as HT_VaiTro_ViewModel;

                System.Windows.Data.CollectionViewSource ht_VaiTro_QuyenHan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_QuyenHan_ListViewModelSource"];
                HT_VaiTro_QuyenHan_ListViewModel ht_VaiTro_QuyenHan_ListViewModel = new HT_VaiTro_QuyenHan_ListViewModel(ht_VaiTro_ViewModel.Model.Khoa);
                ht_VaiTro_QuyenHan_ListViewModel.LoadCompleted += new EventHandler(ht_VaiTro_QuyenHan_ListViewModel_LoadCompleted);
                ht_VaiTro_QuyenHan_ListViewSource.Source = new List<object> { ht_VaiTro_QuyenHan_ListViewModel };

                allExpanded = true;
            }
        }

       
        void ht_VaiTro_QuyenHan_ListViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource ht_VaiTro_QuyenHan_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_QuyenHan_ListViewModelSource"];
            HT_VaiTro_QuyenHan_ListViewModel ht_VaiTro_QuyenHan_ListViewModel = ((List<object>)ht_VaiTro_QuyenHan_ListViewSource.Source)[0] as HT_VaiTro_QuyenHan_ListViewModel;
            IList<HT_VaiTro_QuyenHanInfo> listVaiTroQuyenHan = ht_VaiTro_QuyenHan_ListViewModel.Model.getListOfInfo();
            RadTreeViewItem loopitem = this.HT_QuanLyVaiTroQuyenHan_GeneralInformation_QuyenHanId_RadTreeView.Items[0] as RadTreeViewItem;
            
            while (loopitem != null)
            {
                bool ischecked = false;
                if (loopitem.Item is HT_QuyenHanInfo)
                {
                    foreach (HT_VaiTro_QuyenHanInfo item in listVaiTroQuyenHan)
                    {
                        if (((HT_QuyenHanInfo)loopitem.Item).Khoa == item.QuyenHanId)
                        {
                            ischecked = true;
                            loopitem.CheckState = System.Windows.Automation.ToggleState.On;
                            break;
                        }
                    }
                    if (!ischecked)
                    {
                        loopitem.CheckState = System.Windows.Automation.ToggleState.Off;
                    }
                }
                loopitem = loopitem.NextItem;
            }

        }
        #endregion
    }

}
